/**
 * ____________________ *
 *  ____ ____ ____ _  _ *
 *  |___ [__] |--< |\/| *
 * ____________________ *
 *
 * THE CORM PROJECT
 *
 * This file may only be used in accordance with the
 * terms of the Academic Free License ("AFL") v. 3.0,
 * as published by the Open Software Initiative.  A
 * copy of this license is provided in this source
 * release, and may be viewed online at:
 *
 * (http://www.opensource.org/licenses/afl-3.0.php)
 */
package org.eremite.corm.party.relationship;

import org.apache.openjpa.persistence.ElementType;
import org.apache.openjpa.persistence.FetchGroups;
import org.apache.openjpa.persistence.FetchGroup;
import org.apache.openjpa.persistence.FetchAttribute;
import org.eremite.corm.BaseArchetype;
import org.eremite.corm.party.Party;

import javax.persistence.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Entity
@FetchGroups(
        @FetchGroup(name="detail", attributes={
        @FetchAttribute(name="participants")}))
public class PartyRelationship<P extends Party>
        extends BaseArchetype {

    @ManyToOne(cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE})
    @Column(nullable=false)
    private PartyRelationshipType type;

    @ManyToMany(cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE})
    @ElementType(PartyRole.class)
    private Set<PartyRole<P>> participants =
            new HashSet<PartyRole<P>>();

    /* Default Constructor */
    public PartyRelationship(){}

    /* Convenience Constructor */
    public PartyRelationship(
            PartyRelationshipType type,
            PartyRole<P> ... roles) {
        setPartyRelationshipType(type);
        addParticipant(roles);
    }

    public Set<PartyRole<P>> getParticipants() {
        return participants;
    }

    public void setParticipants(Set<PartyRole<P>> participants) {
        this.participants = participants;
    }

    public PartyRelationshipType getPartyRelationshipType() {
        return this.type;
    }

    public void setPartyRelationshipType(PartyRelationshipType type) {
        this.type = type;
    }

    public void addParticipant(PartyRole<P> role) {
        getParticipants().add(role);
    }

    public void addParticipant(PartyRole<P> ... roles) {
        getParticipants().addAll(Arrays.asList(roles));
    }
}